﻿Public Class Kho
    Private state As Boolean 'true là tạo mới
    Private Sub setReadonly(ByVal istrue As Boolean)
        Me.MaTextEdit.Properties.ReadOnly = istrue
        Me.TenTextEdit.Properties.ReadOnly = istrue
        Me.DiachiTextEdit.Properties.ReadOnly = istrue

    End Sub
    Private Sub BarButtonItemThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemThoat.ItemClick
        Me.Close()
    End Sub

    Private Sub BarButtonItemTaoMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoi.ItemClick
        Try
            Me.setReadonly(False)
            Me.MaTextEdit.Focus()
            Me.KhoBindingSource.AddNew()
            Me.state = True
            Me.BarButtonItemLuu.Enabled = True
            Me.BarButtonItemXoa.Enabled = True
        Catch ex As Exception
        End Try
    End Sub
    Private Sub Frm_DonMau_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'PcvgppDataSet.kho' table. You can move, or remove it, as needed.
        Me.KhoTableAdapter.Fill(Me.PcvgppDataSet.kho)
        setReadonly(True)
        Me.BarButtonItemLuu.Enabled = False
        Me.BarButtonItemXoa.Enabled = False
        Me.IGridView1.BestFitColumns()
    End Sub

    Private Sub BarButtonItemLuu_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuu.ItemClick
        Try
            Me.KhoBindingSource.EndEdit()
            If Me.PcvgppDataSet.HasChanges Then
                If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Lưu' không ?", "NHAC NHO", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then

                    Me.KhoTableAdapter.Update(Me.PcvgppDataSet.kho)
                    Me.state = False
                    Me.setReadonly(True)
                    Me.BarButtonItemLuu.Enabled = False
                    Me.BarButtonItemXoa.Enabled = False
                End If
            Else
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Function checkExisted()
        For index = 0 To Me.IGridView1.RowCount - 1
            Try
                If Me.IGridView1.GetRowCellValue(index, "ma") = Me.MaTextEdit.EditValue Then
                    Return True
                    Exit Function
                End If
            Catch ex As Exception

            End Try
        Next
        Return False
    End Function

    Private Sub MaTextEdit_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        If checkExisted() And state Then
            Me.MaTextEdit.ErrorText = "Mã đã tồn tại"
            e.Cancel = True

        End If
    End Sub

    Private Sub BarButtonItemSua_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemSua.ItemClick
        Me.BarButtonItemLuu.Enabled = True
        Me.BarButtonItemXoa.Enabled = True
        Me.setReadonly(False)
    End Sub

    Private Sub BarButtonItemFirst_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirst.ItemClick
        Me.KhoBindingSource.MoveFirst()
    End Sub

    Private Sub BarButtonItem2_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        Me.KhoBindingSource.MovePrevious()
    End Sub

    Private Sub BarButtonItem6_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem6.ItemClick
        Me.KhoBindingSource.MoveNext()
    End Sub

    Private Sub BarButtonItem7_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem7.ItemClick
        Me.KhoBindingSource.MoveLast()
    End Sub

    Private Sub BarButtonItemXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoa.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Xóa' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.KhoBindingSource.RemoveCurrent()
            Me.KhoTableAdapter.Update(Me.PcvgppDataSet.kho)
            Me.state = False
            Me.setReadonly(True)
            Me.BarButtonItemLuu.Enabled = False
            Me.BarButtonItemXoa.Enabled = False
        End If
    End Sub

    Private Sub EmailTextEdit_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        'If IsDBNull(Me.EmailTextEdit.EditValue) = True Then
        'Else
        '    If Me.EmailTextEdit.EditValue.ToString.Contains("@") = False Then
        '        Me.EmailTextEdit.ErrorText = "Email không hợp lệ"
        '        e.Cancel = True

        '    End If
        'End If

    End Sub

    Private Sub BarButtonItemHuyThayDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoi.ItemClick
        Me.KhoBindingSource.EndEdit()
        If Me.PcvgppDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi đang làm không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PcvgppDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

    End Sub

    Private Sub BarButtonItemIn_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemIn.ItemClick
        Dim report As XtraReportKho = New XtraReportKho
        report.ShowRibbonPreview()

    End Sub
End Class